#include "stdafx.h"
#include <stdio.h>
#include <io.h>
#include "cvxIo.h"

bool CvxIo::travel_dir(const char *dir, std::vector<std::string> &fileName)
{
	long handle;                           //handle for find;
	struct _finddata_t fileinfo;           //struct for file information
	handle=_findfirst(dir, &fileinfo);     //first find
	if(handle == -1){
		fprintf(stderr, "travel_dir can not find dir handle = %s!\n", dir);
		return false;
	}
//	printf("%s\n",fileinfo.name);
	fileName.push_back(std::string(fileinfo.name));
	while(!_findnext(handle,&fileinfo))               
	{
	//	printf("%s\n",fileinfo.name);
		fileName.push_back(std::string(fileinfo.name));
	}
	_findclose(handle); 
	return true;
}
void CvxIo::save_boundary(FILE *fp, const std::vector<CvPoint> &bdy)
{
	int size = bdy.size();
	if (size > 0) {
		fprintf(fp, "%d\n", size);
		for (int i = 0; i<size; ++i) {
			int x = bdy[i].x;
			int y = bdy[i].y;
			fprintf(fp, "%d %d\n", x, y);
		}
		fprintf(fp, "\n");
	}
}
void CvxIo::load_boundary(FILE *fp, std::vector<CvPoint> &bdy)
{
	int size = 0;
	fscanf(fp, "%d\n", &size);
	if (size > 0) {
		bdy.clear();
		for (int i = 0; i<size; ++i) {
			int x = 0;
			int y = 0;
			fscanf(fp, "%d %d\n", &x, &y);
			bdy.push_back(cvPoint(x, y));
		}
	}	
}